// components/materialInquiry/index.js
const common = require('../../utils/common.js');
Component({
  options: {
    styleIsolation: 'shared',
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // 显示-隐藏
    show: {
      type: Boolean,
      value: false,
    },
    multiple: { // 是否多选
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    table_scroll_left: '',
    table_scroll_left_size: '',
    keywords: '',
    materielList: [], // 物料数据
    totalCount: 0,
    formData: {
      orderCode: "", // 来源单号
      spu: "" // 货号
    },
    list_params: {
      needPage: true,
      current: 1,
      size: 20,
      pages: 0,
      parame: [],
      selectParame: [],
      orderBy: "",
      selectParameter: {
        confOrderCategory: "1003",
        spu: '', // 物料编码
        materialName: "", // 物料名称
        supplierName: '' // 供应商名称
      }
    },
    filterShow: false
  },
  created: function () {
    this.fetchData();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
      const __this = this;
      setTimeout(function () {
        __this.setData({
          table_scroll_left: e.detail.scrollLeft
        });
      }, 180);
    },
    fetchData(isAdd) {
      // isAdd 是否分页增加数据
      const _that = this
      console.log(12312);
      common.request_api("/api/erp/purApplyOrder/querySourceOrderCodeList?orderType=MRP_PUR_APPLY", this.data.list_params).then(res => {
        console.log(res);
        let materielList = _that.data.materielList.concat(res.data.records)
        _that.setData({
          materielList: materielList,
          totalCount: res.data.total
        })
      })
    },
    searchChange(events) {
      console.log(events);
      const keywords = events.detail.trim();
      const formData = this.data.formData
      const name = events.currentTarget.dataset.name
      formData[name] = keywords
      const list_params = this.data.list_params
      list_params.current = 1
      list_params.selectParameter = {
        ...list_params.selectParameter,
        ...formData
      }
      this.setData({
        keywords: keywords,
        list_params
      });
    },
    GoSearch(events) {
      this.setData({
        materielList: [],
        'list_params.current': 1,
      });
      this.fetchData()
    },
    handleClearSupplierShortName(events){
      const formData = this.data.formData
      const name = events.currentTarget.dataset.name
      formData[name] = ""
      const list_params = this.data.list_params
      list_params.current = 1
      list_params.selectParameter = {
        ...list_params.selectParameter,
        ...formData
      }
      this.setData({
        keywords: keywords,
        list_params
      });
      this.GoSearch()
    },
    onClose() {
      this.setData({
        show: false
      });
      this.triggerEvent('close');
    },
    onCloseFilterShow() {
      this.setData({
        filterShow: false
      });
    },
    closeFilter() {
      const formData = this.data.formData
      formData.spu = ""
      formData.keywords = ""
      const list_params = this.data.list_params
      list_params.current = 1
      list_params.selectParameter = {
        ...list_params.selectParameter,
        ...formData
      }
      this.setData({
        list_params,
        formData
      })
    },
    saveFilter() {
      this.setData({
        filterShow: false,
        materielList: [],
      })
      this.fetchData()
    },
    handleSelect(e) {
      const row = e.currentTarget.dataset.row
      this.triggerEvent('handleSelect', {
        row
      });
      this.onClose()
    },
    nextPage(e) {
      if (e.detail.direction !== "bottom") {
        return
      }
      if (!(this.data.totalCount <= this.data.materielList.length)) {
        const current = this.data.list_params.current + 1
        this.setData({
          'list_params.current': current,
        });
        this.fetchData()
      }
    },
    onChangeisChecked(event) {
      console.log(event);
      const detail = event.detail
      const index = event.currentTarget.dataset.index
      const materielList = this.data.materielList.map((item, i) => {
        if (index === i) {
          item.isChecked = detail
        }
        return {
          ...item
        }
      })
      this.setData({
        materielList
      })
    },
    // 批量选择
    handleBatchSelect() {
      const materielList = this.data.materielList.filter((item, i) => {
        return item.isChecked
      })
      this.triggerEvent('handleSelect', materielList);
      this.onClose()
    },
    showImg(e) {
      wx.showLoading()
      let img = e.currentTarget.dataset.img;
      wx.previewImage({
        current: img, // 图片的地址url
        urls: [img]
      })
      wx.hideLoading()
    },
    showPopup() {
      this.setData({
        filterShow: true
      })
    }
  }
})